const Koa = require('koa');

const app = new Koa();

const bodyparser = require('koa-bodyparser');

app.use(bodyparser());

const Router = require('koa-router');

const router = new Router();

app.use(router.routes());

app.use(router.allowedMethods());

const hmc = require('./util/hmc');

const query = require('./db/query');

//注册
router.post('/api/registry',async (ctx) => {
    // ctx.request.url 简写成 ctx.url
    //ctx.request.body 不能简写 ctx.body
    //ctx.reponse.body 简写  ctx.body
    let {username,password} = ctx.request.body;
    //容错处理
    if(/\d{11}/.test(username) && /\w{6,20}/.test(password)){
        //1.password 不能明文展示  需要加密
        let hmcPwd = hmc(password);
        console.log(hmcPwd);
        //2.排重[]
        let data = await query('select * from userlist where username=?',[username]);
        if(data.length){
            //说明此人已经存在
            ctx.body = {
                code:3,
                msg:'此用户名存在'
            }
        }else{
            //说明此人不存在
            try{
                await query('insert into userlist (username,password) values (?,?)',[username,hmcPwd]);
                ctx.body = {
                    code:1,
                    msg:'注册成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})
//接口  获取验证码
//ctx.body = 123

//登录
router.post('/api/login',async (ctx,next) => {
    let {username,password} = ctx.request.body;
    if(/\d{11}/.test(username) && /\w{6,20}/.test(password)){
        //加密
        let hmcPwd = hmc(password);
        let data = await query('select * from userlist where username=? and password=?',[username,hmcPwd]);

        if(data.length){
            ctx.body = {
                code:1,
                msg:'登录成功'
            }
        }else{
            ctx.body = {
                code:0,
                msg:'登录失败'
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})


//签到接口
router.post('/api/sign',async (ctx,next) =>{
    let {uid} = ctx.request.body;
    let today = new Date();
    let year = today.getFullYear();

    let month = today.getMonth()+1;

    let day = today.getDate(); //天

    let time = `${year}-${month}-${day}`;
    console.log(time)

    if(uid){
        //排重  //已经签到了就不能再签
        let sign_info = await query('select * from sign_list where uid=? and sign_time=?',[uid,time]);
        if(sign_info.length){
            ctx.body = {
                code:3,
                msg:'今日已签到'
            }
        }else{
            try{
                await query('insert into sign_list (sign_time,uid) values (?,?)',[time,uid]);
                ctx.body = {
                    code:1,
                    msg:'签到成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }

})

app.listen(3000,() => {
    console.log("服务启动成功")
})